﻿
var WBEM = COM.createObject( "WbemScripting.SWbemLocator.1" );
var wmi = WBEM.ConnectServer( ".", "root/cimv2" );

var my_net_in = 0;
var my_net_out = 0;
var my_net_in_last = 0;
var my_net_out_last = 0;
var my_net_in_diff = 0;
var my_net_out_diff = 0;
var my_net_bw = 1;

var chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function GenerateDeviceList() {
	// This is a horrible, horrible way to iterate through a loop...
	// Unfortunately, we don't really get a count of interfaces, so this is the best we can do
	for(var i=0;i<26;i++) {
		try {
			if ( TestDevice(chrs[i] + ":") > 0 ) {
				print("Drive: " + chrs[i]);
				net_NAME[i] = chrs[i] + ":";
				if(net_USED == -1) net_USED = i;
			}

		} catch (e) { }
	}
	return true;
}

function GetDeviceIN() {
	my_net_in = wmi.Get( 'Win32_PerfRawData_PerfDisk_LogicalDisk="' + net_NAME[net_USED] + '"' ).Properties_.Item( "DiskReadBytesPerSec" ).Value;

	if(my_net_in_last==0) my_net_in_last = my_net_in;
	my_net_in_diff = my_net_in - my_net_in_last;
	my_net_in_last = my_net_in;

	return my_net_in_diff/mod_discmonitor_timer.interval;
}

function GetDeviceOUT() {
	my_net_out = wmi.Get( 'Win32_PerfRawData_PerfDisk_LogicalDisk="' + net_NAME[net_USED] + '"' ).Properties_.Item( "DiskWriteBytesPerSec" ).Value;

	if(my_net_out_last==0) my_net_out_last = my_net_out;
	my_net_out_diff = my_net_out - my_net_out_last;
	my_net_out_last = my_net_out;

	return my_net_out_diff/mod_discmonitor_timer.interval;
}

function GetDeviceBW() {
	my_net_bw = 1;
	return my_net_bw;
}

function TestDevice(path) {
	return wmi.Get( 'Win32_PerfRawData_PerfDisk_LogicalDisk="' + path + '"' ).Properties_.Item( "DiskReadBytesPerSec" ).Value;
}
